package com.foolishbird.sdk.params;



import com.foolishbird.sdk.secret.Base64;
import com.foolishbird.sdk.secret.MD5;
import com.foolishbird.sdk.utils.RSAUtil;
import com.foolishbird.sdk.utils.WL;
import com.foolishbird.sdk.utils.XmlUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.URLEncoder;


public  class PostRequestParams {

    private static final String TAG = PostRequestParams.class.getSimpleName();

     String service;

    final String sign;

    long time;

    final JSONObject data;
    private boolean isFloat =false;

    private PostRequestParams(final Builder builder) {
//        this.time = builder.time;
        this.sign = builder.sign;
//        this.service = builder.service;
        this.data = builder.data;
    }

    private PostRequestParams(final FloatBuilder builder) {
        this.time = builder.time;
        this.sign = builder.sign;
        this.service = builder.service;
        this.data = builder.data;
        this.isFloat = true;
    }

    public static PostRequestParams createRequestParams(RequestDataParams data) {

        return new Builder(data).build();
    }

    public static PostRequestParams createFloatRequestParams(BaseFloatRequestParams data) {

        return new FloatBuilder(data).build();
    }

    public String toParamsString() {
        WL.e(TAG, "toParamsString ---");

        if(!isFloat){
            try {
                data.getJSONObject("base_fb").getJSONObject("validation_info").put("sign",sign);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            WL.e(TAG, "toParamsString ---"+ data.toString());
//
//            try {
//                String base = Base64.encodeToString(RSAUtil.encryptByPublicKey(data.toString().getBytes()),Base64.DEFAULT);
//
//                base = URLEncoder.encode(base, "UTF-8");
//                WL.e(TAG, "encode ---"+ base);
////            byte[] src = Base64.decode("W2n8b0ApNQiAZw9FXV761xXKRRNtrn8TRT1Jaa4btTai3AASl+K6jgxrskj3b4jh1ozQL8mlTVypkdKUSpCPpuY/YJixCCZuPCdafvuAgNSrGmfH98xkOs71Prh8bPqovjeBS8D8lE1QCShlY3hMvzJUFm/DwUFVU3QhIeNZiU4A0luBtTmPrPsCwJOaT98TmNAM96q/FRJQ3f0Uizgmpw3W1lncJEUMdEnZvBCxHpgb3wVB4SrI6HpMxuG4/cH24zJfM18RMroKnp4WWXEIA+RYCkWf4RCIs/+KiKaX3Cb2Rwh7vHU11lVmb6FqqwXy5reGhNWz9fme+c5iwba+hw==",Base64.DEFAULT);
////            byte[] aaa = RSAUtil.decryptByPrivateKey(src);
////            WL.e(TAG, "decode -default--"+ new String(aaa));
//                return base.toString();
//            }catch (Exception e){
//                e.printStackTrace();
//                WL.e(TAG, "decode ---"+ "error");
//            }


            return data.toString();
        }else{
            JSONObject jsonStr = new JSONObject();
            try {
                jsonStr.put("data", data);
                jsonStr.put("service", service);
                jsonStr.put("sign", sign);
                jsonStr.put("time", time);
            } catch (JSONException e) {
                WL.e(TAG, "toParamsString ---> JSONException");
            }



            return jsonStr.toString();
//            WL.e(TAG, "toParamsString ---"+ jsonStr.toString());
//            return jsonStr.toString();
        }
    }

    public static class Builder {
//        private String service;
        private String sign;
//        private long time;
        private JSONObject data;

        public Builder(RequestDataParams data) {
//            this.service = data.getServiceString();
            this.data = data.toJsonObject();
            WL.d(TAG, "data to string: " + data.toString());
//            this.time = System.currentTimeMillis() / 1000;
            StringBuilder builder = new StringBuilder()
//                    .append(service)
//                    .append(time)
                    .append(data.toString())
                    .append("key="+XmlUtils.getPlatformSecret());
            WL.d(TAG, "Builder sign string: " + builder.toString());
            this.sign = MD5.encrypt(builder.toString()).toLowerCase();
        }
        public PostRequestParams build() {
            return new PostRequestParams(this);
        }

    }

    public static class FloatBuilder {
        private String service;
        private String sign;
        private long time;
        private JSONObject data;

        public FloatBuilder(BaseFloatRequestParams data) {
            this.service = data.getServiceString();
            this.data = data.toJsonObject();
            this.time = System.currentTimeMillis() / 1000;
            StringBuilder builder = new StringBuilder()
                    .append(service).append(time)
                    .append(data.toString())
                    .append(XmlUtils.getPlatformCustomKey());
            WL.d(TAG, "Builder sign string: " + builder.toString());
            this.sign = MD5.encrypt(builder.toString()).toLowerCase();
        }

        public PostRequestParams build() {
            return new PostRequestParams(this);
        }

    }
}
